<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="{{ url_for('static', filename='styles.css') }}">
    <title>论坛</title>
</head>
<body>
    <h1>论坛</h1>
    <form id="postForm">
        <textarea id="postContent" placeholder="在此输入帖子内容..." required></textarea>
        <button type="submit">发布</button>
    </form>
    <div id="posts">
        {% for post in posts %}
            <div class="post">
                <p><strong>{{ post.author }}</strong>: {{ post.content }}</p>
            </div>
        {% endfor %}
    </div>

    <script>
        document.getElementById('postForm').addEventListener('submit', async (event) => {
            event.preventDefault();
            const content = document.getElementById('postContent').value;
            const author = "用户";  // 可替换为实际用户

            const response = await fetch('/forum/post', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ content, author })
            });

            if (response.ok) {
                window.location.reload();
            }
        });
    </script>
</body>
</html>